package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;

import javax.script.ScriptException;
import java.io.FileWriter;
import java.sql.SQLException;
import java.util.*;

/**
 * 增量和展期记录(台账展期记录)
 */
@BusinessDataConverter(
        insertSql = "insert into nd_account_extension_schedule (ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, PAY_NO, ORIG_ENDDATE, ACCOUNT_BALANCE, EXTENSION_ENDDATE, EFFECTIVE_DATE, Bank, EXTENSION_REMARK, PROCID)" +
                "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE  nd_account_extension_schedule SET CREATE_NAME = ?,CREATE_BY = ?,CREATE_DATE = ?,UPDATE_NAME = ?,UPDATE_BY = ?,UPDATE_DATE = ?,SYS_ORG_CODE = ?,SYS_COMPANY_CODE = ?,BPM_STATUS = ?,PRO_ID = ?,PAY_NO = ?,ORIG_ENDDATE = ?,ACCOUNT_BALANCE = ?,EXTENSION_ENDDATE = ?,EFFECTIVE_DATE = ?,Bank = ?,EXTENSION_REMARK = ?,PROCID = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT ID FROM ND_ACCOUNT_EXTENSION_SCHEDULE WHERE PRO_ID = ?"
)
public class NdAccountExtensionSchedule extends AbstractBusiness{
    private static StrongUuidGenerator uuidGenerator = new StrongUuidGenerator();
    public NdAccountExtensionSchedule(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void findMigratedIdList() throws SQLException {
    }

    @Override
    public void migrate() throws ScriptException, SQLException, Exception {
        List<Map> BtItemAccounDeferRecList = sourceData.getBtItemAccountDeferRec();
        String DeleteSql ="DELETE FROM ND_ACCOUNT_EXTENSION_SCHEDULE WHERE PRO_ID = ?";
        this.targetDbUtil.updateByParams(DeleteSql, Arrays.asList(new String[]{this.projectId}));
        for(Map btItemAccounDeferRec : BtItemAccounDeferRecList){
            try {

                    this.targetDbUtil.updateByParams(insertSql, this.buildValues(btItemAccounDeferRec));

            } catch (Exception e) {
                throw new Exception(String.format("增量和展期记录(台账展期记录)[%s]迁移失败:%s\n", btItemAccounDeferRec.get("ITEMNO"), e.getMessage()));
            }

        }
    }
    public List buildValues (Map btItemAccounDeferRec) throws SQLException, ScriptException {
        List values = new ArrayList();
        //ID	主键
        values.add(uuidGenerator.getNextId());
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemAccounDeferRec.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemAccounDeferRec.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add(null);
        //PRO_ID	项目ID
        values.add(btItemAccounDeferRec.get("ITEMNO"));
        //PAY_NO	放款编号
        values.add(btItemAccounDeferRec.get("ACCOUNTNO"));
        //ORIG_ENDDATE	原到期时间
        values.add(btItemAccounDeferRec.get("OLDENDDATE"));
        //ACCOUNT_BALANCE	账目余额
        values.add(btItemAccounDeferRec.get("ASKDEFERSUM"));
        //EXTENSION_ENDDATE	展期结束时间
        values.add(btItemAccounDeferRec.get("DEFERENDDATE"));
        //EFFECTIVE_DATE	生效时间
        values.add(btItemAccounDeferRec.get("VAILABLEDATE"));
        // Bank	合作银行
        values.add(DictTool.getTargetDictCode(Bank.class,btItemAccounDeferRec.get("BANKID")));
        //EXTENSION_REMARK	展期备注
        values.add(Tool.decodeUnicode(btItemAccounDeferRec.get("DEFERMEMO")));
        //PROCID	流程id
        values.add(btItemAccounDeferRec.get("WORKFLOWNO") == null  ? null :btItemAccounDeferRec.get("WORKFLOWNO")+"-JW");
        
        return values;
        
    }
}

